package origami;

import origami.Node.*;
/**
 * Created by Tzachi on 06/11/2014.
 *
 * The same function I wrote during the phone interview
 * Using extra 3 bits of color in order to keep track on visiting order and back edges
 */

public class ColorRecursiveMethod implements ICycleRecognition {
    public boolean findLoop(Node startNode) {
        if (startNode == null) {
            return false;
        }
        startNode.setColor(Color.GREY);
        for (Node node : startNode.getChildren()) {
            if (Color.GREY.equals(node.getColor())) {
                return true;
            }
            if (Color.WHITE.equals(node.getColor())) {
                if (findLoop(node)) {
                    return true;
                }
            }
        }
        startNode.setColor(Color.BLACK);
        return false;
    }


}
